import API from '../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    max: 384,
    currentWordNumber: 0,
    tempFilePaths: [],
    upPicList: [],
    content: '',
    address: ''
  },
  onLoad: function (options) {
    this.getLocation()
  },
  onShow: function () {

  },
  upPic() {
    var _this = this
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        _this.setData({
          tempFilePaths: tempFilePaths
        })
        for (var k in tempFilePaths) {
          _this.uploadFile(tempFilePaths[k])
        }
      }
    })
  },
  uploadFile(img) {
    console.log(img)
    var _this = this
    wx.showLoading({ title: '图片上传中...' })
    wx.uploadFile({
      url: 'https://www.szhongtu.net/mcbase/public/index.php/wxroutine/quanzi/upload',
      filePath: img,
      name: 'file',
      formData: {
        'user': 'test'
      },
      success(res) {
        wx.hideLoading()
        var data = JSON.parse(res.data)
        if (data.ret == 0) {
          var pic = _this.data.upPicList
          pic.push(data.url)
          _this.setData({
            upPicList: pic
          })
        }
      }
    })
  },
  delPic(e) {
    var index = e.currentTarget.dataset.index
    var picArray = this.data.tempFilePaths
    picArray.splice(index, 1)
    this.setData({
      tempFilePaths: picArray
    })
  },
  selectCircle() {
    wx.navigateTo({
      url: '/pages/join/join',
    });
  },
  questionCon(e) {
    console.log(e.detail.value)
    var len = parseInt(e.detail.value.length);
    this.setData({
      content: e.detail.value,
      currentWordNumber: len
    })
  },
  // 获取当前位置经维度
  getLocation() {
    var _this = this
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy
        _this.mapInfo(longitude, latitude)
      }
    })
  },
  mapInfo(longitude, latitude) {
    var _this = this
    console.log(location)
    wx.request({
      url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + latitude + ',' + longitude + '&key=VT6BZ-C5LKP-5KLDX-VJFAK-7S326-AMBQO',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log('获取城市')
        console.log(res.data.result.address)
        _this.setData({
          address: res.data.result.address,
          latitude: latitude,
          longitude: longitude
        })


      }
    })
  },

  publishQuestion() {
    if (this.data.content == "") {
      wx.showModal({
        title: '',
        content: '请输入问题的具体描述',
      })
      return
    }
    var data = {
      content: this.data.content,
      title: this.data.title,
      address: this.data.address,
      latitude: this.data.latitude,
      longitude: this.data.longitude,
      kindid: 2,
      circleid: '2,3',
      images: this.data.upPicList.join(',')
    }
    API.publishQuestion(data).then(res => {
      if (res.ret == 0) {
        wx.showToast({
          title: '发布成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(() => {
          wx.navigateTo({
            url: '/pages/questionDetail/questionDetal?id=' + res.data
          })
        }, 2000)
      }

    })
  }

})